package zth;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

public class AddUsers extends HttpServlet {
    private static final String INSERT_TEMPLATE =
            "insert into information (account,password,sex,type) " +
                    "values('%s','%s','%s','%s')";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        String password = request.getParameter("password");
        String sex = request.getParameter("gender");
        String type = request.getParameter("type");

        //char number='0';

        String sql = String.format(INSERT_TEMPLATE,account,password,sex,type);
        //System.out.println(sql);
//        PrintWriter writer = response.getWriter();
//        writer.println("here!");
//        writer.flush();//ok！

        try {
//            PrintWriter writer = response.getWriter();
//            writer.println("hera!");
//            writer.flush();//ok！
            DBUtils.insert(sql);
            response.sendRedirect("./login.html");
        } catch (SQLException e) {
            e.printStackTrace();
        }

        }
        //response.sendRedirect("./login.html");
    }

